package dtos

import (
	"github.com/edgexfoundry/edgex-go/internal/app/models"
	"github.com/edgexfoundry/go-mod-core-contracts/v2/dtos"
	edgexModel "github.com/edgexfoundry/go-mod-core-contracts/v2/models"
)

type DeviceCommand struct {
	dtos.DBTimestamp   `json:",inline"`
	dtos.DeviceCommand `json:",inline"`
	Description        string              `json:"description,omitempty"`
	Code               string              `json:"code,omitempty"`
	Id                 string              `json:"id,omitempty"`
	ResourceOperations []ResourceOperation `json:"resourceOperations,omitempty"`
}

type ResourceOperation struct {
	dtos.ResourceOperation `json:",inline"`
	UUID                   string         `json:"uuid,omitempty"`
	ResourceCode           string         `json:"resourceCode,omitempty"`
	Resource               DeviceResource `json:"resource,omitempty"`
}

func (d *DeviceCommand) ToDeviceCommandModel() models.DeviceCommand {
	return models.DeviceCommand{
		DBTimestamp: edgexModel.DBTimestamp{
			Created:  d.Created,
			Modified: d.Modified,
		},
		DeviceCommand:      dtos.ToDeviceCommandModel(d.DeviceCommand),
		Code:               d.Code,
		Description:        d.Description,
		Id:                 d.Id,
		ResourceOperations: ToResourceOperation(d.ResourceOperations),
	}
}

func ToDeviceCommandModels(dcs []DeviceCommand) []models.DeviceCommand {
	commands := make([]models.DeviceCommand, len(dcs))
	for index, ro := range dcs {
		commands[index] = ro.ToDeviceCommandModel()
	}
	return commands
}

func (d *ResourceOperation) ToResourceOperationModel() models.ResourceOperation {
	return models.ResourceOperation{
		ResourceOperation: dtos.ToResourceOperationModel(d.ResourceOperation),
		UUID:              d.UUID,
		ResourceCode:      d.ResourceCode,
		Resource:          d.Resource.ToDeviceResourceModel(),
	}
}

func ToResourceOperation(ros []ResourceOperation) []models.ResourceOperation {
	resourceOperations := make([]models.ResourceOperation, len(ros))
	for index, ro := range ros {
		resourceOperations[index] = ro.ToResourceOperationModel()
	}
	return resourceOperations
}
